﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public int[,] arr = null;
        int index = 0;
        public List<Point> lines = new List<Point>();
        private Boolean end = false;

        public string cAr = "";
        public MainWindow()
        {
            InitializeComponent();
            initArr();
            DrawCanvas();
            startMove();
        }

        void startMove(){
            move(0, 0, 5, 5);
        }

        void move(int sx, int sy, int tx, int ty)
        {
            if (end) return;
            if (sx < 0 || sx > 9 || sy < 0 || sy > 9) return;
            if (isBlock(sx, sy)) return;
            if (HasMoved(sx, sy)) return;
            if (isEnd(sx, sy, tx, ty))
            {
                end = true;
                return;
            }
            lines.Add(new Point(sx, sy));
            System.Diagnostics.Trace.WriteLine("x:" + sx + " y:" + sy);
            GoNext(sx, sy, tx, ty);
        }

        bool HasMoved(int x, int y)
        {
            foreach (Point point in lines) {
                if (point.X == x && point.Y == y)
                    return true;
            }
            return false;
        }

        bool isEnd(int sx, int sy, int tx, int ty) {
            if ((tx == sx) && (ty == sy)) return true;
            return false;
        }


        void GoNext(int x, int y,int tx,int ty)
        {
            DrawRect(x, y, 0);
            int addx = 1;
            if (tx < x) addx = -1;
            int addy = 1;
            if (ty < y) addy = -1;

            if (!isBlock(x + addx, y)) {
                //如果在同一x坐标上
                if (ty == y) move(x + addx, y, tx, ty);
                if (tx == x) move(x, y + addy, tx, ty);
                //x向目标一步无障碍（假设目标在起始点右下方）
                move(x + addx, y, tx, ty);  //往前走
                if (isBlock(x + addx, y + addy)) { //右下方有障碍，往下走
                    move(x, y + addy, tx, ty);
                }
                if (isBlock(x + addx, y - addy)){   //右上方有障碍，往上走
                    move(x, y - addy, tx, ty);
                }
                if (isBlock(x - addx, y + addy)) {  //左下方有障碍，往下走，往后走
                    move(x, y + addy, tx, ty);
                    move(x - addx, y, tx, ty);
                }
                if (isBlock(x - addx, y - addy))
                {     //左上方有障碍，往上走,往后走
                    move(x, y - addy, tx, ty);
                    move(x - addx, y, tx, ty);
                }

            }else{  //若前方有障碍
                if (!isBlock(x, y + addy)) { //优先向下走
                    move(x, y + addy, tx, ty);
                }

                if (!isBlock(x, y - addy)) { //向上走
                    move(x, y - addy, tx, ty);
                }
            }

        }

        bool isBlock(int x, int y) {
            if (x < 0 || x > 9 || y < 0 || y > 9) return true;
            return arr[x, y] == 1;
        }

        private void DrawCanvas()
        {
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                    if(arr[i,j] == 1) DrawRect(i,j,1);
        }

        private void initArr() {
            arr = new int[10, 10];
            for (int i = 0; i < 10; i++){
                for (int j = 0; j < 10; j++)
                {
                    arr[i, j] = 0;
                    DrawRect(i, j,2);
                }
            }
            index = 0;

            for (int i = 1; i < 9; i++)
                arr[i, 1] = 1;
            for (int i = 2; i < 8; i++)
                arr[8, i] = 1;
            for (int i = 1; i < 9; i++)
                arr[i, 8] = 1;
            for (int i = 3; i < 8; i++)
                arr[1, i] = 1;
            for (int i = 2; i < 7; i++)
                arr[i, 3] = 1;
            for (int i = 4; i < 7; i++)
                arr[6, i] = 1;
            for (int i = 3; i < 6; i++)
                arr[i, 6] = 1;
            //for (int i = 4; i < 5; i++)
            //arr[3, 4] = 1; 
            arr[3, 5] = 1;
        }

        public void DrawRect(int x, int y,int c)
        {
            int rectSize = 50;
            Rectangle rect = new Rectangle();
            if (c == 1) rect.Fill = Brushes.Black;
            else if (c == 2) rect.Fill = Brushes.LightGreen;
            else rect.Fill = Brushes.Gold;
            rect.Width = rectSize-2;
            rect.Height = rectSize-2;
            Canvas.SetTop(rect, y * rectSize-1);
            Canvas.SetLeft(rect, x * rectSize-1);
            canvas.Children.Add(rect);

            Label label = new Label();
            label.Content = Convert.ToString(index);
            Canvas.SetTop(label, (y * rectSize - 1));
            Canvas.SetLeft(label, (x * rectSize - 1));
            canvas.Children.Add(label);
            index++;
        }


    }

}
